/**
 * 表格中的图片的长度/宽度
 * @type {number}
 */
let size = 45;

/**
 * 表格工具栏高度
 * @type {number}
 */
let heigt_tool = 33;

/**
 * 表格标题的高度
 * @type {number}
 */
let heigt_title = 32;

/**
 * 表格头部的高度
 * @type {number}
 */
let heigt_header = 32;

/**
 * 表格过滤工具栏的高度
 * @type {number}
 */
let heigt_filter = 32;

/**
 * 表格行的高度
 * @type {number}
 */
let heigt_tr = 32;

/**
 * 表格行的高度（包含高度为size的图片）
 * @type {number}
 */
let heigt_tr_with_image = 50;

/**
 * 包含角色详情列的表格行的高度（包含高度为size的图片）
 * @type {number}
 */
let heigt_tr_with_role_details = 55;

/**
 * 表格滚动条的高度
 * @type {number}
 */
let heigt_scoll_bar = 18;

/**
 * 表格脚部的高度
 * @type {number}
 */
let heigt_footer = 35;

/**
 * 删除小图标
 * @type {string}
 */
let imageUrl = "/easyui/themes/icons/delete.png";
let image_delete = "<img title='删除' src='" + imageUrl + "' />";

/**
 * 修改图片按钮
 * @type {{handler: handler, text: string, iconCls: string}}
 */
let imageUpdateButton = {
    iconCls: "icon-upload",
    text: "修改图片",
    handler: function() {
        imageHandler();
    }
};

/**
 * 表格删除列
 * @type {{formatter: (function(*, *, *): string), field: string, title: string, align: string}}
 */
let deleteColumn = {
    title: "操作",
    field: "right",
    align: "center",
    formatter: function(value, rowData, rowIndex) {
        return "<a href='javascript:void(0);' onclick='deleteHandler(" + rowData.id + ")'>"
            + image_delete + "</a>";
    }
}

/**
 * 角色详情列
 * @type {{formatter: (function(*=, *, *): string), field: string, width: number, title: string, align: string}}
 */
let roleDetailsColumn = {
    field: "roleId",
    title: "归属角色",
    align: "center",
    width: 160,
    formatter: function(value, rowData, rowIndex) {
        ajaxGet("/role_account/selectRoleDetails", {
            id: value
        }, function(resp) {
            let data = resp.data;

            $("#role_name_" + rowIndex).html(data.name)
            $("#role_grade_" + rowIndex).html(data.grade);
            $("#role_image_" + rowIndex).prop("src", data.roleImage).attr("title", data.roleName);
            $("#role_school_" + rowIndex).prop("src", data.schoolImage).attr("title", data.schoolName);
        }, error);

        let html = "<div class='center'>";

        html += 	"<table>";
        html += 	    "<tr>";
        html += 		    "<td rowspan='2'>";
        html += 			    "<img height='" + size + "' id='role_image_" + rowIndex + "' />";
        html += 		    "</td>";

        html += 		    "<td colspan='2'>";
        html += 			    "<span id='role_name_" + rowIndex + "' />";
        html += 		    "</td>";
        html += 	    "</tr>";

        html += 	    "<tr>";
        html += 		    "<td>";
        html += 			    "<img height='" + size / 2 + "' id='role_school_" + rowIndex + "' />";
        html += 		    "</td>";

        html += 		    "<td>";
        html += 			    "<span id='role_grade_" + rowIndex + "' />级";
        html += 		    "</td>";
        html += 	    "</tr>";
        html += 	"</table>";

        html += "</div>";

        return html;
    }
};

/**
 * 渲染文件上传文件框
 * @param selector 选择器
 * @param requestUrl 请求路径
 */
function fetchUploadFilebox(selector, requestUrl) {
    $("#upload").filebox({
        buttonText: "选择文件",
        width: 240,
        required: true,
        onChange: function() {
            let rowData = $(selector).datagrid("getSelected");

            if(rowData) {
                fileUpload(this, rowData.id, requestUrl);
            }
        }
    });
}

/**
 * 渲染文件上传对话框
 * @param requestUrl 请求路径
 * @param callback 请求成功的额外回调方法
 */
function fetchUploadDialog(requestUrl, callback) {
    $("#upload_dialog").dialog({
        title: "修改图片",
        width: 500,
        height: 300,
        modal: true,
        closed: true,
        closable: false,
        draggable: false,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#upload_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);
                        resetValue("#upload");

                        $("#img").attr("src", "");
                        $(selector).form("clear");
                        $("#upload_dialog").dialog("close");
                        callback();
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                resetValue("#upload");

                $("#img").attr("src", "");
                $("#upload_form").form("clear");
                $("#upload_dialog").dialog("close");
            }
        }]
    });
}

/**
 * 验证手机号格式
 * @param phone
 * @returns {boolean}
 */
function isPhone(phone) {
    // 最新的手机号正则表达式
    let phone_number_reg = /^1[3-9]\d{9}$/;

    return phone_number_reg.test(phone);
}

/**
 * 验证邮箱格式
 * @param email
 * @returns {boolean}
 */
function isEmail(email) {
    // 邮箱正则表达式
    let email_reg = /^(([^()[\]\\.,;:\s@"]+(\.[^()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    return email_reg.test(email);
}

/**
 * 判断数组中是否包含指定元素
 * @param array 任意数组
 * @param element 数组元素
 */
function contains(array, element) {
    if ($.isArray(array)) {
        let exists = false;

        for (let i = 0; i < array.length; i++) {
            if (array[i] === element) {
                exists = false;
                break;
            }
        }

        return exists;
    } else {
        throw new Error("参数array必须是数组！");
    }
}